package org.cainiao.scenario.highconcurrency.config;

import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 配置类中初始化限流器
@Configuration
public class RateLimiterConfig {

    @Bean
    public RRateLimiter seckillRateLimiter(RedissonClient redissonClient) {
        RRateLimiter rateLimiter = redissonClient.getRateLimiter("seckillRateLimiter");
        // 设置速率：每秒10000个令牌（整体限流）
        rateLimiter.trySetRate(RateType.OVERALL, 10000, 1, RateIntervalUnit.SECONDS);
        return rateLimiter;
    }
}
